<template>
  <el-dialog title="bom版本"
    v-if="showFlag"
    :visible.sync="showFlag"
    :modal= false
    width="50%"
    center
  >
    <el-row>
      <el-col>
        <el-table v-loading="loading" :data="bomVersionList" @current-change="handleCurrent" @row-dblclick="handleRowDbClick">
          <el-table-column width="50" align="center" >
              <template v-slot="scope">
                  <el-radio v-model="bomVersionId" :label="scope.row.bomVersionId" @change="handleRowChange(scope.row)">{{""}}</el-radio>
              </template>
          </el-table-column>
          <el-table-column label="bom版本号"   align="center" key="bomVersion" prop="bomVersion" v-if="columns[1].visible" >
          </el-table-column>

          <el-table-column label="创建时间" align="center" prop="createTime" v-if="columns[2].visible"  >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmSelect">确 定</el-button>
        <el-button @click="showFlag=false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listMdItem, getMdItem, delMdItem, addMdItem, updateMdItem} from "@/api/mes/md/mdItem";
import { listAllUnitmeasure} from "@/api/mes/md/unitmeasure";
import {genCode} from "@/api/system/autocode/rule"
import { getToken } from "@/utils/auth";
import { treeselect } from "@/api/mes/md/itemtype";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

import {bomVersionList} from "@/api/mes/md/bom";

export default {
  name: "MdItemSingle",
  dicts: ['sys_yes_no','mes_item_product'],
  components: { Treeselect },
  props: {
    itemId: String,
  },
  data() {
    return {
      bomVersion: undefined,
      showFlag:false,
      // 选中数组
      bomVersionId: undefined,
      selectedRows: undefined,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料产品表格数据
      bomItemList:null,

      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemCode: undefined,
        itemName: undefined,
        itemTypeId: 0,
        mdItemId: this.itemId,
        bomVersionId: undefined
      },
      // 列信息
      columns: [
        { key: 0, label: `bom版本号主键`, visible: true },
        { key: 1, label: `bom版本号`, visible: true },
        { key: 2, label: `创建时间`, visible: true }
      ]
    };
  },
  watch: {
    // 根据名称筛选分类树
    itemTypeName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getTreeselect();
  },
  methods: {
    /** 查询物料编码列表 */
    // getList() {
    //   this.loading = true;
    //   listMdItem(this.queryParams).then(response => {
    //       this.itemList = response.rows;
    //       this.total = response.total;
    //       this.loading = false;
    //     }
    //   );
    // },
    showDialog() {
      this.showFlag = true;
      this.getList();
    },
    getList() {
      this.queryParams.mdItemId=this.itemId;
      this.loading = true;
      console.log('bomVersionList itemId----',this.itemId);
      console.log('this.queryParams ----',this.queryParams);
      bomVersionList(this.queryParams).then(response => {
            this.bomVersionList = response.rows;
            this.total = response.total;
            this.loading = false;
          }
      );
    },
    getList1(itemId) {
      this.queryParams.mdItemId=itemId;
      this.loading = true;
      bomVersionList(this.queryParams).then(response => {
            this.bomVersionList = response.rows;
            this.total = response.total;
            this.loading = false;
          }
      );
    },
    /** 查询分类下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.itemTypeOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.itemTypeId = data.id;
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleCurrent(row){
        if(row){
            this.selectedRows = row;
        }
    },
    handleRowDbClick(row){
      if(row){
        this.selectedRows = row;
        this.$emit('onSelected',this.selectedRows);
        this.showFlag = false;
      }
    },
    // 单选选中数据
    handleRowChange(row) {
      if(row){
        this.selectedRows = row;
      }
    },
    //确定选中
    confirmSelect(){
        if(this.bomVersionId ==null || this.bomVersionId==0){
            this.$notify({
                title:'提示',
                type:'warning',
                message: '请至少选择一条数据!'
            });
            return;
        }
        this.$emit('onSelected',this.selectedRows);
        this.showFlag = false;
    }
  }
};
</script>
